#include "main.h"

QueueNode  *head;
QueueNode  *tail;

QueueNode  *su_head;
QueueNode  *su_tail;

int is_empty(void)
{
	return head == NULL;
}

int su_is_empty(void)
{
	return su_head == NULL;
}

void su_enqueue(QUEUE_TYPE value)
{
	QueueNode   *new_node;
	new_node = (QueueNode *)malloc(sizeof(QueueNode));
	if (NULL == new_node)
	{
		log_error("malloc failure\n");
		return ;
	}

	memset(new_node->data, 0, sizeof(new_node->data));
	strncpy(new_node->data, value, sizeof(new_node->data));

	if (NULL == head)
	{
		su_head = new_node;
		su_tail = new_node;
	}
	else
	{
		su_tail->next = new_node;
		su_tail = new_node;
	}
}

void enqueue(QUEUE_TYPE value)
{
	QueueNode   *new_node;
	new_node = (QueueNode *)malloc(sizeof(QueueNode));
	if (NULL == new_node)
	{
		log_error("malloc failure\n");
		return ;
	}

	memset(new_node->data, 0, sizeof(new_node->data));
	strncpy(new_node->data, value, sizeof(new_node->data));

	if (NULL == head)
	{
		head = new_node;
		tail = new_node;
	}
	else
	{
		tail->next = new_node;
		tail = new_node;
	}
}

void dequeue(void)
{
	if (!is_empty())
	{
		QueueNode *front_node;
		front_node = head;
		head = front_node->next;
		free(front_node);
		
		if (NULL == head)
		{
			tail == NULL;
		}
	}
	
	return ;
}

void su_dequeue(void)
{
	if (!su_is_empty())
	{
		QueueNode *front_node;
		front_node = su_head;
		su_head = front_node->next;
		free(front_node);
		
		if (NULL == su_head)
		{
			su_tail == NULL;
		}
	}

	return ;
}

QUEUE_TYPE get_head(void)
{
	if (!is_empty())
	{
		return head->data;
	}
	
	return NULL;
}

QUEUE_TYPE su_get_head(void)
{
	if (!su_is_empty())
	{
		return su_head->data;
	}

	return NULL;
}

QUEUE_TYPE get_tail(void)
{
	assert(!is_empty());
	return tail->data;
}

QUEUE_TYPE get_su_tail(void)
{
	assert(!su_is_empty());
	return su_tail->data;
}

void empty_queue(void)
{
	while(!is_empty())
		dequeue();
}

void su_empty_queue(void)
{
	while(!su_is_empty())
		su_dequeue();
}
